//
//  DateExtension.swift
//  MGExtensionsKit
//
//  Created by Max on 2019/8/6.
//

import Foundation

extension Date {
    public init(dateString: String?, format: String?) {
        if let _ = dateString, let _ = format {
            let formatter = DateFormatter()
            formatter.dateFormat = format!
            if let date = formatter.date(from: dateString!) {
                self.init(timeInterval: 0, since: date)
                return
            }
        }
        self.init()
    }
    
    /// 是否同一天
    public func isInSameDay(to other: Date) -> Bool {
        var calendar = Calendar(identifier: Calendar.Identifier.gregorian)
        if let timeZone = TimeZone(identifier: "Asia/Shanghai") {
            calendar.timeZone = timeZone
        }
        let components: Set<Calendar.Component> = [.month, .day, .hour]
        let startComponents = calendar.dateComponents(components, from: self)
        let endComponents = calendar.dateComponents(components, from: other)
        if let month = startComponents.month, let otherMonth = endComponents.month, let day = startComponents.day, let otherDay = endComponents.day, month==otherMonth, day==otherDay {
            return true
        }
        return false
    }
    
    /// 是否今天
    public func isInToday() -> Bool {
        var calendar = Calendar(identifier: Calendar.Identifier.gregorian)
        if let timeZone = TimeZone(identifier: "Asia/Shanghai") {
            calendar.timeZone = timeZone
        }
        return calendar.isDateInToday(self)
    }
    
    /// 是否明天
    public func isTomorrow() -> Bool {
        var calendar = Calendar(identifier: Calendar.Identifier.gregorian)
        if let timeZone = TimeZone(identifier: "Asia/Shanghai") {
            calendar.timeZone = timeZone
        }
        return calendar.isDateInTomorrow(self)
    }
    
    public var isInThisYear: Bool {
        let now = Calendar.current.dateComponents([.year], from: Date())
        let other = Calendar.current.dateComponents([.year], from: self)
        return now.year == other.year
    }
    
    /// 获取日历对象
    public func calendarComponents() -> DateComponents {
        var calendar = Calendar(identifier: Calendar.Identifier.gregorian)
        if let timeZone = TimeZone(identifier: "Asia/Shanghai") {
            calendar.timeZone = timeZone
        }
        let components: Set<Calendar.Component> = [.month, .day, .hour, .hour, .minute, .second, .weekday]
        return calendar.dateComponents(components, from: self)
    }
}

/// String 相关
extension Date {
    /// 中文星期几
    
    public var weekdayInChinese: String? {
        
        guard let weekday = calendarComponents().weekday else {
            return nil
        }
        
        switch weekday {
        case 1:
            return "星期日"
        case 2:
            return "星期一"
        case 3:
            return "星期二"
        case 4:
            return "星期三"
        case 5:
            return "星期四"
        case 6:
            return "星期五"
        case 7:
            return "星期六"
        default:
            return nil
        }
        
    }
    
    /// 获取格式化字符串
    public func dateString(format: String) -> String {
        let formatter = DateFormatter()
        formatter.dateFormat = format
        return formatter.string(from: self)
    }
    
    /// 私信消息时间
    public func chatTimeString() -> String {
        let dateFormatter = DateFormatter()
        dateFormatter.timeZone = TimeZone(identifier: "Asia/Shanghai")
        
        if Calendar.current.isDateInToday(self) {
            // 今天
            dateFormatter.dateFormat = "HH:mm"
            return "今天 \(dateFormatter.string(from: self))"
        } else if Calendar.current.isDateInYesterday(self) {
            // 昨天
            dateFormatter.dateFormat = "HH:mm"
            return "昨天 \(dateFormatter.string(from: self))"
        } else if isInThisYear {
            // 今年
            dateFormatter.dateFormat = "MM-dd HH:mm"
            return dateFormatter.string(from: self)
        } else {
            // 今年以前
            dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
            return dateFormatter.string(from: self)
        }
    }
    
    /// 社区时间展示
    public func messageTimeString() -> String {
        let time = Date().timeIntervalSince(self)
        if time < 60 {
            return "刚刚"
        } else if time < 60*60 {
            return "\(Int(time)/60)分钟前"
        } else if time < 24*60*60 {
            return "\(Int(time)/3600)小时前"
        } else if time < 3*24*60*60 {
            return "\(Int(time)/86400)天前"
        } else if isInThisYear {
            // 今年
            let dateFormatter = DateFormatter()
            dateFormatter.timeZone = TimeZone(identifier: "Asia/Shanghai")
            dateFormatter.dateFormat = "MM-dd"
            return dateFormatter.string(from: self)
        } else {
            // 今年以前
            let dateFormatter = DateFormatter()
            dateFormatter.timeZone = TimeZone(identifier: "Asia/Shanghai")
            dateFormatter.dateFormat = "yyyy-MM-dd"
            return dateFormatter.string(from: self)
        }
    }
}
